/*
 *
 * FGLRender.java
 *
 * Created by Wuwang on 2016/9/29
 */
package com.example.mydemo

import android.opengl.GLES32
import com.example.mydemo.base.IBaseTexture
import java.util.LinkedList

class MyRender(private val texture: IBaseTexture) : IMyRender {

    private val mPreDrawTask = LinkedList<Runnable>() //绘制之前的任务

    override fun onSurfaceCreated() {
        GLES32.glClearColor(1f, 1f, 1f, 1f)
        texture.onSurfaceCreated()
    }

    override fun onSurfaceChanged(width: Int, height: Int) {
        //glViewport- 设置视口
        GLES32.glViewport(0, 0, width, height)
        texture.onSurfaceChanged(width, height)
    }

    override fun onDrawFrame(): Boolean {
        GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT or GLES32.GL_DEPTH_BUFFER_BIT)
        runPreDrawTask()
        texture.onDrawFrame()
        return false
    }

    override fun onSurfaceError() {
    }

    override fun onDestroy() {
    }

    private fun addPreDrawTask(runnable: Runnable) {
        synchronized(mPreDrawTask) { mPreDrawTask.add(runnable) }
    }

    private fun runPreDrawTask() {
        synchronized(mPreDrawTask) {
            while (!mPreDrawTask.isEmpty()) {
                mPreDrawTask.poll()?.run()
            }
        }
    }
}